<template>
  <div class="data-update">
    <el-card>
      <template #header>
        <span>数据更新</span>
      </template>
      <el-table :data="funds" style="width: 100%">
        <el-table-column label="名称">
          <template #default="scope">
            {{ scope.row.name }}
            <span style="color: #909399; font-size: 12px; margin-left: 4px;">({{ scope.row.code }})</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="调用时间" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="primary" @click="callApi(scope.row)">调用api</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const funds = ref([])

const fetchFunds = async () => {
  const res = await axios.get('/api/funds')
  // 兼容后端返回对象或数组，直接赋值，不覆盖updateTime
  const list = Array.isArray(res.data) ? res.data : (res.data?.data || [])
  funds.value = list
}

const callApi = async (row) => {
  row.updateTime = '请求中...'
  try {
    await axios.post('/api/call', { code: row.code })
    row.updateTime = new Date().toLocaleString()
  } catch {
    row.updateTime = '调用失败'
  }
}

onMounted(fetchFunds)
</script>

<style scoped>
.data-update {
  padding: 24px;
}
</style>
